<template>
  <div>
    <el-table stripe class="bao_tableStyle" :data="fileListData">
      <el-table-column label="序号" type="index" width="80" align="center" />
      <el-table-column v-if="fileTypeBool" show-overflow-tooltip label="文件类型" prop="annexName" align="left" width="150">
        <template slot-scope="scope">
          <span class="ellipsisSpan-1">{{ decodeURI(scope.row.annexName) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="文件名称" show-overflow-tooltip prop="annexName" align="left" min-width="200">
        <template slot-scope="scope">
          <span class="ellipsisSpan-1">{{ decodeURI(scope.row.annexName) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="上传人" show-overflow-tooltip prop="createName" align="left" width="200">
        <template slot-scope="scope">
          <span class="ellipsisSpan-1">{{ decodeURI(scope.row.createName) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="文件大小" show-overflow-tooltip prop="annexSize" align="left" width="120" :formatter="filterSize" />
      <el-table-column label="上传时间 " show-overflow-tooltip align="left" prop="createDate" width="190">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createDate) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" :fixed="earlyBool?'':'right'" align="left" width="170">
        <template slot-scope="scope">
          <!-- <el-button type="text" @click="fileLook(scope.row)">查看</el-button> -->
          <el-button type="text" @click="downLoadFileFun(scope.row)">下载</el-button>
          <el-button v-if="(deleteBool&&(scope.row.createBy==user.userInfo.userId))" typeC="danger" type="text"
                     @click="deleteFun(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
      <!-- 列表缺省 -->
      <div slot="empty" class="zjm_no_data_table_list height-100 flex row-center col-center">
        <el-empty :image-size="70" description="暂无数据"></el-empty>
      </div>
    </el-table>
  </div>
</template>
<script>
import { methodPostApi } from '@/api/publicApi'
import { uploadMixins } from '@/mixins/publicMixins/uploadMixins'
export default {
  mixins: [uploadMixins],
  props: {
    fileListData: Array,
    fileTypeBool: Boolean,
    deleteBool: {
      type: Boolean,
      default: true// 是否可删除
    },
    earlyBool: {
      type: Boolean,
      default: false// 是不是前期管理的附件
    }
  },
  data() {
    return {
      downAnnexName: [],
      deleteData: [],
      createByBool: true,
      tablelist: true
    }
  },
  computed: {
    user() {
      return this.$store.state.user
    }
  },
  created() {
  },
  methods: {
    deleteFun(row) {
      if (!this.earlyBool) {
        if (row.editBool) {
          this.fileListData.forEach((element, index) => {
            if (element.id === row.id) {
              this.fileListData.splice(index, 1)
              this.$emit('deleteFileList', element, 'deleteAnnexIds')
              this.msgAlt('删除成功', 'success')
            }
          })
        } else {
          this.deleteFileFun(row, this.fileListData)
        }
      } else {
        const ids = []
        ids.push(row.id)
        methodPostApi('/sys/annex/removeByIds', ids.toString()).then(res => {
          this.fileListData.forEach((element, index) => {
            if (element.id === row.id) {
              this.fileListData.splice(index, 1)
              this.msgAlt('删除成功', 'success')
            }
          })
        }).catch(() => {
          this.msgAlt('删除失败', 'error')
        })
      }
    },
    // 文件大小转换
    filterSize(row) {
      const size = row.annexSize
      if (!size) return ''
      if (size < this.pow1024(1)) return size + ' B'
      if (size < this.pow1024(2)) return (size / this.pow1024(1)).toFixed(2) + ' KB'
      if (size < this.pow1024(3)) return (size / this.pow1024(2)).toFixed(2) + ' MB'
      if (size < this.pow1024(4)) return (size / this.pow1024(3)).toFixed(2) + ' GB'
      return (size / this.pow1024(4)).toFixed(2) + ' TB'
    },
    pow1024(num) {
      return Math.pow(1024, num)
    }
  }
  // beforeDestroy() {
  //   alert("4n!")
  // },
}
</script>
